﻿<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_block_front_FreeContentAction extends f_core_Action
{
	public function execute()
	{
        $contentId = $this->getBlockParameter("contentId");
        $lang = $this->getBlockParameter("blockLang");
        $revisionId = $this->getBlockParameter("revision");
        $preview = $this->getBlockParameter("preview");
        $published = $this->getBlockParameter("published");
        $class = $this->getBlockParameter("class");
        $pageId = $this->getContext('currentPage')->getUniqueId();

		$tempDocService = m_backoffice_services_TempDocService::getInstance();

		if($preview == "true" && $published == 0 && $tempDocService->hasTemporary($contentId, $lang))
		{
			$doc = $tempDocService->getTemporaryDocument($contentId, $lang);
		}
		else
		{
            if($this->isTemplateCached("front/block/FreeContent.html", $pageId.'_'.$contentId, $lang."|content|free") && $preview != "true")
            {
                return $this->getTemplateCacheContent();
            }        

			if($revisionId != "")
			{
				$doc = $this->getDocumentProvider()->getByModelAndUniqueId('website/freeContent', $contentId, $lang);

                if($doc->getLastRevisionNumber() != $revisionId)
                {
                    $doc = $doc->getRevision(intval($revisionId));
                }

                $revisionId = null; // for cache, only use id and lang not revision id
			}
			else
			{   
				$doc = $this->getDocumentProvider()->getByModelAndUniqueId('website/freeContent', $contentId, $lang);

				if($doc->isRevisable() && count($doc->getRevisions()) > 0 && intval($revisionId) == 0)
				{
					$doc = f_document_Provider::getInstance()->getByModelAndUniqueId('website/FreeContent', $contentId, $lang)->getRevision(0);
				}

                $revisionId = null;
			}
		}

		if(!is_null($doc))
		{
            $content = preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&amp;',$doc->getContent());
            $content = html_entity_decode(stripslashes($doc->getContent()), ENT_COMPAT, 'utf-8');
			$this->addToModel("content", $content);
		}

		$this->addToModel("class", $class);
		$this->addToModel("preview", $preview);
		$this->addToModel("lang", $lang);
		$this->addToModel("contentId", $contentId);
		$this->addToModel("revisionId", $revisionId);
		$this->addToModel("pageId", $pageId);

		$this->setView('success');
	}	
	
	public function validate()
	{
		return true;
	}
	
	public function errorHandler()
	{
		$this->setView('error');
	}
}